import { CreateElement, VNode } from 'vue';

import { isFunction, getControl } from '../vendors/handie';

function isComponentCtor(ctor: any): boolean {
  return isFunction(ctor) && !!(ctor as any).extendOptions;
}

function kebabCase(str: string): string {
  const resolved = str.replace(/([A-Z])/g, '-$1').toLowerCase();

  return resolved.charAt(0) === '-' ? resolved.slice(1) : resolved;
}

function createNode(h: CreateElement, componentName: string, ...args: any[]): VNode {
  const node = h(getControl(componentName), ...args);

  if (node.componentOptions && !node.componentOptions.tag) {
    node.componentOptions.tag = kebabCase(componentName); // hack for some situations like https://github.com/view-design/ViewUI/blob/v4.6.1/src/components/select/select.vue#L122
  }

  return node;
}

export { isComponentCtor, createNode };
